import { HashService } from '@app/hash';
import { PrismaService } from '@app/prisma';
import { UseGuards } from '@nestjs/common';
import { Args, Int, Mutation, Resolver } from '@nestjs/graphql';
import { AccountPower, GqlAuthPowerGuard } from 'src/auth/auth.guard';
import { CountDto } from './count.dto';

@Resolver()
export class UserResolver {
    constructor(
        private readonly prisma: PrismaService,
        private readonly hash: HashService
    ) { }

    @Mutation(() => CountDto)
    @UseGuards(new GqlAuthPowerGuard(AccountPower, 3))
    async userDeleteManyByids(
        @Args("ids", { type: () => [Int] }) ids: number[]
    ) {
        await this.prisma.userTaskItem.deleteMany({
            where: {
                userOnTaskUserId: {
                    in: ids
                }
            }
        })
        await this.prisma.userOnTask.deleteMany({
            where: {
                userId: {
                    in: ids
                }
            }
        })
        return await this.prisma.user.deleteMany({
            where: {
                id: {
                    in: ids
                }
            }
        })
    }

    @Mutation(() => CountDto)
    @UseGuards(new GqlAuthPowerGuard(AccountPower, 3))
    async userResetManyByids(
        @Args("ids", { type: () => [Int] }) ids: number[]
    ) {
        const users = await this.prisma.user.findMany({
            where: {
                id: {
                    in: ids
                }
            },
            include: {
                profile: true
            }
        })
        let total = 0;
        for (const user of users) {
            await this.prisma.profile.update({
                where: {
                    id: user.profile.id,
                },
                data: {
                    password: this.hash.cryptoPassword(user.account)
                }
            })
            total++;
        }
        return new CountDto(total)
    }
}
